package com.spr.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.spr.enums.SprDevicePushEventTypeEnum;
import com.spr.mapper.SprDeviceCompleteRecordMapper;
import com.spr.mapper.SprDeviceInfoMapper;
import com.spr.mapper.SprDevicePartInfoMapper;
import com.spr.mapper.SprDevicePushRecordMapper;
import com.spr.pojo.SprDeviceCompleteRecord;
import com.spr.pojo.SprDeviceInfo;
import com.spr.pojo.SprDevicePartInfo;
import com.spr.pojo.SprDevicePushRecord;
import com.spr.request.SprDevicePaintingCompleteRecord;
import com.spr.request.SprDevicePaintingRecord;
import com.spr.service.SprDevicePushService;
import com.spr.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

import static com.spr.enums.SprDevicePushEventTypeEnum.RECOED;

@Slf4j
@Service
public class SprDevicePushServiceImpl  implements SprDevicePushService {
    @Autowired
    private SprDeviceInfoMapper sprDeviceInfoMapper;
    @Autowired
    private SprDeviceCompleteRecordMapper sprDeviceCompleteRecordMapper;
    @Autowired
    private SprDevicePartInfoMapper sprDevicePartInfoMapper;
    @Autowired
    private SprDevicePushRecordMapper sprDevicePushRecordMapper;

    @Override
    public void devicePaintingCompleteRecord(SprDevicePaintingCompleteRecord sprDevicePaintingCompleteRecord) {

        saveSprDeviceInfo(sprDevicePaintingCompleteRecord);
        SprDeviceCompleteRecord sprDeviceCompleteRecord =buildSprDeviceCompleteRecord(sprDevicePaintingCompleteRecord);
        SprDevicePartInfo sprDevicePartInfo =buildSprDevicePartInfo(sprDevicePaintingCompleteRecord);
        SprDevicePushRecord sprDevicePushRecord=buildSprDevicePushRecord(sprDevicePaintingCompleteRecord, RECOED);
        sprDeviceCompleteRecordMapper.insert(sprDeviceCompleteRecord);
        sprDevicePartInfoMapper.insert(sprDevicePartInfo);
        sprDevicePushRecordMapper.insert(sprDevicePushRecord);

    }

    private SprDevicePushRecord buildSprDevicePushRecord(SprDevicePaintingCompleteRecord sprDevicePaintingCompleteRecord, SprDevicePushEventTypeEnum recoed) {
        SprDevicePushRecord sprDevicePushRecord=new SprDevicePushRecord();
        sprDevicePushRecord.setDeviceNumber(sprDevicePaintingCompleteRecord.getDeviceNumber());
        long days = DateUtils.differentDays(sprDevicePaintingCompleteRecord.getEndTime(), sprDevicePaintingCompleteRecord.getBeginTime());
        sprDevicePushRecord.setOperationNumber(days);
        sprDevicePushRecord.setOperationStartTime(sprDevicePaintingCompleteRecord.getBeginTime());
        sprDevicePushRecord.setPushTime(sprDevicePaintingCompleteRecord.getEndTime());
        switch (recoed){
            case RECOED:
                sprDevicePushRecord.setCurrentStatus(1);
                break;
            case EVENT:
                sprDevicePushRecord.setCurrentStatus(2);
                break;
            case HEART:
                sprDevicePushRecord.setCurrentStatus(1);
                break;
        }
        sprDevicePushRecord.setDeviceNumber(sprDevicePaintingCompleteRecord.getDeviceNumber());
        sprDevicePushRecord.setCreateTime(new Date());
        sprDevicePushRecord.setCreateBy(sprDevicePaintingCompleteRecord.getOperator());
        sprDevicePushRecord.setEventType(SprDevicePushEventTypeEnum.getEventTypeString(recoed));
        return sprDevicePushRecord;
    }

    @Override
    public void devicePaintingRecord(SprDevicePaintingRecord sprDevicePaintingRecord) {
        log.info("SprDevicePushServiceImpl,devicePaintingRecord,sprDevicePaintingRecord:{}", JSON.toJSON(sprDevicePaintingRecord));
        if (StringUtils.isBlank(sprDevicePaintingRecord.getType())){
            return;
        }
        SprDevicePushEventTypeEnum eventTypeEnum = SprDevicePushEventTypeEnum.getEnumByString(sprDevicePaintingRecord.getType());
        if (Objects.isNull(eventTypeEnum)){
            return;
        }
        switch (eventTypeEnum){
            case RECOED:
                saveRecordType(sprDevicePaintingRecord,RECOED);
                break;
            case EVENT:
                saveEventType(sprDevicePaintingRecord,SprDevicePushEventTypeEnum.EVENT);
                break;
            case HEART:
                saveHeartType(sprDevicePaintingRecord,SprDevicePushEventTypeEnum.HEART);
                break;

        }


    }

    private void saveRecordType(SprDevicePaintingRecord sprDevicePaintingRecord, SprDevicePushEventTypeEnum recoed) {
        JSONObject datas = sprDevicePaintingRecord.getDatas();
        SprDevicePaintingCompleteRecord sprDevicePaintingCompleteRecord = JSONObject.toJavaObject(datas, SprDevicePaintingCompleteRecord.class);
        if (Objects.nonNull(sprDevicePaintingCompleteRecord)){
            saveSprDeviceInfo(sprDevicePaintingCompleteRecord);
            SprDeviceCompleteRecord sprDeviceCompleteRecord =buildSprDeviceCompleteRecord(sprDevicePaintingCompleteRecord);
            SprDevicePartInfo sprDevicePartInfo =buildSprDevicePartInfo(sprDevicePaintingCompleteRecord);
            SprDevicePushRecord sprDevicePushRecord=buildSprDevicePushRecord(sprDevicePaintingCompleteRecord, RECOED);
            sprDeviceCompleteRecordMapper.insert(sprDeviceCompleteRecord);
            sprDevicePartInfoMapper.insert(sprDevicePartInfo);
            sprDevicePushRecordMapper.insert(sprDevicePushRecord);
        }

    }

    private void saveHeartType(SprDevicePaintingRecord sprDevicePaintingRecord , SprDevicePushEventTypeEnum recoed) {
        JSONObject datas = sprDevicePaintingRecord.getDatas();
        SprDevicePaintingCompleteRecord sprDevicePaintingCompleteRecord = JSONObject.toJavaObject(datas, SprDevicePaintingCompleteRecord.class);
        if (Objects.nonNull(sprDevicePaintingCompleteRecord)){
            saveSprDeviceInfo(sprDevicePaintingCompleteRecord);
            SprDevicePartInfo sprDevicePartInfo =buildSprDevicePartInfo(sprDevicePaintingCompleteRecord);
            SprDevicePushRecord sprDevicePushRecord=buildSprDevicePushRecord(sprDevicePaintingCompleteRecord,recoed);
            sprDevicePartInfoMapper.insert(sprDevicePartInfo);
            sprDevicePushRecordMapper.insert(sprDevicePushRecord);
        }
    }

    private void   saveEventType(SprDevicePaintingRecord sprDevicePaintingRecord , SprDevicePushEventTypeEnum recoed){
        JSONObject datas = sprDevicePaintingRecord.getDatas();
        SprDevicePaintingCompleteRecord sprDevicePaintingCompleteRecord = JSONObject.toJavaObject(datas, SprDevicePaintingCompleteRecord.class);
        if (Objects.nonNull(sprDevicePaintingCompleteRecord)){
            saveSprDeviceInfo(sprDevicePaintingCompleteRecord);
            SprDevicePartInfo sprDevicePartInfo =buildSprDevicePartInfo(sprDevicePaintingCompleteRecord);
            SprDevicePushRecord sprDevicePushRecord=buildSprDevicePushRecord(sprDevicePaintingCompleteRecord,recoed);
            sprDevicePartInfoMapper.insert(sprDevicePartInfo);
            sprDevicePushRecordMapper.insert(sprDevicePushRecord);
        }
    }


    private SprDevicePartInfo buildSprDevicePartInfo(SprDevicePaintingCompleteRecord sprDevicePaintingCompleteRecord) {
        SprDevicePartInfo sprDevicePartInfo=new SprDevicePartInfo();
        sprDevicePartInfo.setDeviceNumber(sprDevicePaintingCompleteRecord.getDeviceNumber());
        sprDevicePartInfo.setDeviceName(sprDevicePaintingCompleteRecord.getDeviceName());
        sprDevicePartInfo.setPartBrand(sprDevicePaintingCompleteRecord.getBrand());
        sprDevicePartInfo.setPartBrandType(sprDevicePaintingCompleteRecord.getBrandType());
        sprDevicePartInfo.setPartDirection(sprDevicePaintingCompleteRecord.getDirection());
        sprDevicePartInfo.setPartOperator(sprDevicePaintingCompleteRecord.getOperator());
        sprDevicePartInfo.setPartPaintName(sprDevicePaintingCompleteRecord.getPaintName());
        sprDevicePartInfo.setPartPaintType(sprDevicePaintingCompleteRecord.getPaintType());
        sprDevicePartInfo.setUpdateTime(new Date());
        sprDevicePartInfo.setCreateTime(new Date());
        return sprDevicePartInfo;
    }

    private SprDeviceCompleteRecord buildSprDeviceCompleteRecord(SprDevicePaintingCompleteRecord sprDevicePaintingCompleteRecord) {
        SprDeviceCompleteRecord sprDeviceCompleteRecord = new SprDeviceCompleteRecord();
        sprDeviceCompleteRecord.setCompletionTime(sprDevicePaintingCompleteRecord.getEndTime());
        sprDeviceCompleteRecord.setDeviceNumber(sprDevicePaintingCompleteRecord.getDeviceNumber());
        long days = DateUtils.differentDays(sprDevicePaintingCompleteRecord.getEndTime(), sprDevicePaintingCompleteRecord.getBeginTime());
        sprDeviceCompleteRecord.setOperationNumber(days);
        sprDeviceCompleteRecord.setOperationStartTime(sprDevicePaintingCompleteRecord.getBeginTime());
        sprDeviceCompleteRecord.setCreateTime(new Date());
        sprDeviceCompleteRecord.setCreateBy(sprDevicePaintingCompleteRecord.getOperator());
        return sprDeviceCompleteRecord;
    }

    private void saveSprDeviceInfo(SprDevicePaintingCompleteRecord sprDevicePaintingCompleteRecord) {
        SprDeviceInfo sprDeviceInfo=new SprDeviceInfo();
        SprDeviceInfo existSprDeviceInfo = sprDeviceInfoMapper.selectOne(new LambdaQueryWrapper<SprDeviceInfo>()
                .eq(SprDeviceInfo::getDeviceNumber, sprDevicePaintingCompleteRecord.getDeviceNumber())
                .orderByDesc(SprDeviceInfo::getCreateTime)
                .last("limit 1")
        );
        if (Objects.nonNull(existSprDeviceInfo)){
            existSprDeviceInfo.setUpdateTime(new Date());
            existSprDeviceInfo.setUpdateBy(sprDevicePaintingCompleteRecord.getOperator());
            BigDecimal devicePrice = existSprDeviceInfo.getDevicePrice();
            Integer partNumber = existSprDeviceInfo.getPartNumber();
            if (Objects.nonNull(partNumber)){
                partNumber++;
                existSprDeviceInfo.setPartNumber(partNumber);
            }
            sprDeviceInfoMapper.updateById(existSprDeviceInfo);
        }else {
                    sprDeviceInfo.setCreateBy(sprDevicePaintingCompleteRecord.getOperator());
                    sprDeviceInfo.setUpdateBy(sprDevicePaintingCompleteRecord.getOperator());
                    sprDeviceInfo.setCreateTime(new Date());
                    sprDeviceInfo.setUpdateTime(new Date());
                    sprDeviceInfo.setDeviceName(sprDevicePaintingCompleteRecord.getDeviceName());
                    sprDeviceInfo.setDeviceNumber(sprDevicePaintingCompleteRecord.getDeviceNumber());
                    sprDeviceInfo.setSiteName("龙华区环观南路一汽大众吴天林观澜店");
                    sprDeviceInfo.setSiteNumber("Fhnfr8DL8d");
                    sprDeviceInfo.setIsComplete(Boolean.TRUE);
                    sprDeviceInfo.setPartNumber(1);
                    sprDeviceInfo.setDevicePrice(BigDecimal.ZERO);
            sprDeviceInfoMapper.insert(sprDeviceInfo);

        }





    }


}
